package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.HttpClient;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.github.wxpay.sdk.WXPayUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("/api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @SneakyThrows
    @RequestMapping("/wxNotifyAddress")
    public String wxNotifyAddress(HttpServletRequest request) {
        //获取输入流
        ServletInputStream inputStream = request.getInputStream();
        //使用byte输出流接收数据，因为http请求是以字节流的形式传输的
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //声明字节数组作为缓冲区，用来暂存每次从输入流中读取的数据。
        byte[] buffer = new byte[1024];
        //记录每次读取的长度
        int len = 0;
        //从输入流中读取数据，直到输入流中没有数据可读取
        while ((len = inputStream.read(buffer)) != -1){
            outputStream.write(buffer,0, len);
        }
        //输出流转换为字节数组
        byte[] byteArray = outputStream.toByteArray();
        //将数组转换为字符串
        String xmlString = new String(byteArray);

        //将字符串转换为map
        Map<String, String> result = WXPayUtil.xmlToMap(xmlString);

        //修改支付流水记录
        CompletableFuture.runAsync(()->{
            wxPayService.updatePaymentInfo(result);
        });
        System.out.println(result);
        //关闭流
        inputStream.close();
        //清洗流的数据
        outputStream.flush();
        //关闭流
        outputStream.close();
        //初始化返回对象
        Map<String ,String> resultMap = new HashMap<>();
        resultMap.put("return_code","SUCCESS");
        resultMap.put("return_msg","OK");
        return WXPayUtil.mapToXml(resultMap);

//        String xmlString =  "<xml><appid><![CDATA[wx74862e0dfcf69954]]></appid>\n" +
//                "<bank_type><![CDATA[OTHERS]]></bank_type>\n" +
//                "<cash_fee><![CDATA[1]]></cash_fee>\n" +
//                "<fee_type><![CDATA[CNY]]></fee_type>\n" +
//                "<is_subscribe><![CDATA[N]]></is_subscribe>\n" +
//                "<mch_id><![CDATA[1558950191]]></mch_id>\n" +
//                "<nonce_str><![CDATA[f8a3815775ca44a699be32a0ac5fc534]]></nonce_str>\n" +
//                "<openid><![CDATA[oHwsHuAZvBwhmE4TfxG-_tt75alk]]></openid>\n" +
//                "<out_trade_no><![CDATA[b3a72a177f160275e1b1f2cd02bbb857]]></out_trade_no>\n" +
//                "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
//                "<return_code><![CDATA[SUCCESS]]></return_code>\n" +
//                "<sign><![CDATA[505DDE66F2D1E42BD339F80709C81E09]]></sign>\n" +
//                "<time_end><![CDATA[20240515112356]]></time_end>\n" +
//                "<total_fee>1</total_fee>\n" +
//                "<trade_type><![CDATA[NATIVE]]></trade_type>\n" +
//                "<transaction_id><![CDATA[4200002201202405150930314538]]></transaction_id>\n" +
//                "</xml>";
    }

}
